﻿
Partial Class WebForm_MyPager
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        'AddHandler Me.gv.PageIndexChanged, AddressOf Me.gv_PageIndexChanged
        AddHandler Me.gv.DataBound, AddressOf Me.gv_DataBound
        'AddHandler Me.gv.DataBinding, AddressOf Me.gv_DataBinding
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Me.BindDDLPageIndex()
            'Me.BindPageButton()
        End If
    End Sub

    Protected Sub gv_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim gv As GridView = sender

        Me.BindDDLPageIndex(gv.PageIndex)
        Me.BindPageButton()
    End Sub

    Protected Sub gv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim gv As GridView = sender

        Me.BindDDLPageIndex(gv.PageIndex)
        Me.BindPageButton()
    End Sub

    Protected Sub ddlPageIndex_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPageIndex.SelectedIndexChanged
        Dim ddl As DropDownList = TryCast(sender, DropDownList)

        Dim args As New GridViewCommandEventArgs(ddl, New CommandEventArgs("Page", ddl.SelectedValue))
        Me.RaiseBubbleEvent(ddl, args)

    End Sub

End Class

Partial Class WebForm_MyPager
    Private ReadOnly Property gv() As GridView
        Get
            Dim parent As Control = Me.Parent
            While parent IsNot Nothing
                If TypeOf parent Is GridView Then
                    Return parent
                End If

                parent = parent.Parent
            End While

            Return Nothing
        End Get
    End Property

    Public ReadOnly Property TotalRowCount() As Integer
        Get
            If TypeOf Me.gv.DataSource Is System.Data.DataTable Then
                Return TryCast(Me.gv.DataSource, System.Data.DataTable).Rows.Count
            ElseIf TypeOf Me.gv.DataSource Is System.Data.DataSet Then
                Return TryCast(Me.gv.DataSource, System.Data.DataSet).Tables(0).Rows.Count
            ElseIf TypeOf Me.gv.DataSource Is System.Data.DataView Then
                Return TryCast(Me.gv.DataSource, System.Data.DataView).Table.Rows.Count
            ElseIf Me.gv.DataSourceObject IsNot Nothing Then
                Try
                    Return TryCast(Me.gv, My.MyGridView).TotalRowCount
                Catch ex As Exception
                    Try
                        Return TryCast(Me.gv, TServerControl.Web.TGridView).TotalRowCount
                    Catch
                        Return 0
                    End Try
                End Try
            End If
        End Get
    End Property


    Public Sub BindDDLPageIndex()
        BindDDLPageIndex(0)
    End Sub

    Public Sub BindDDLPageIndex(ByVal pageIndex As Integer)

        If gv Is Nothing Then
            Return
        End If

        If Me.ddlPageIndex.Items.Count = 0 Then
            For i As Integer = 1 To Me.gv.PageCount
                Me.ddlPageIndex.Items.Add(i)
            Next

        End If

        Me.ddlPageIndex.Items(pageIndex).Selected = True
    End Sub

    Public Sub BindPageButton()
        If gv Is Nothing Then
            Return
        End If

        If Me.ddlPageIndex.SelectedValue = 1 Then
            'Me.btnFirst.Enabled = False
            Me.btnFirst.ForeColor = Drawing.Color.LightGray
            Me.btnFirst.Attributes("onclick") = "alert('It is the first page!!');return false;"
            'Me.btnPrev.Enabled = False
            Me.btnPrev.ForeColor = Drawing.Color.LightGray
            Me.btnPrev.Attributes("onclick") = "alert('It is the first page!!');return false;"
        Else
            Me.btnFirst.Enabled = True
            Me.btnPrev.Enabled = True
        End If

        If Me.ddlPageIndex.SelectedValue = Me.ddlPageIndex.Items.Count Then
            'Me.btnLast.Enabled = False
            Me.btnLast.ForeColor = Drawing.Color.LightGray
            Me.btnLast.Attributes("onclick") = "alert('It is the last page!!');return false;"
            'Me.btnNext.Enabled = False
            Me.btnNext.ForeColor = Drawing.Color.LightGray
            Me.btnNext.Attributes("onclick") = "alert('It is the last page!!');return false;"
        Else
            Me.btnLast.Enabled = True
            Me.btnNext.Enabled = True
        End If

        Me.lblRowCountInfo.Text = String.Format("Page <b>{0}</b>/{1}, {2} records", Me.ddlPageIndex.SelectedValue, Me.ddlPageIndex.Items.Count, Me.TotalRowCount)

        RaiseEvent PageButtonRefreshed(Me, New EventArgs)
    End Sub

    Public Event PageButtonRefreshed(ByVal sender As Object, ByVal e As EventArgs)

    Public Property RowCountInfo() As String
        Get
            Return Me.lblRowCountInfo.Text
        End Get
        Set(ByVal value As String)
            Me.lblRowCountInfo.Text = value
        End Set
    End Property

End Class
